package com.makeunion.gdlbsdemo.activity;

import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Toast;

import com.amap.api.maps.AMap;
import com.amap.api.maps.MapView;
import com.amap.api.maps.model.BitmapDescriptorFactory;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.MultiPointItem;
import com.amap.api.maps.model.MultiPointOverlay;
import com.amap.api.maps.model.MultiPointOverlayOptions;
import com.makeunion.gdlbsdemo.R;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

/**
 * 类说明
 *
 * @author renjialiang
 * @version [版本]
 * @see [相关类]
 * @since [模块]
 */
public class MultiPointsActivity extends AppCompatActivity {

    private MapView mapView;

    private AMap aMap;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_multi_points);

        mapView = (MapView) findViewById(R.id.map_view);
        mapView.onCreate(savedInstanceState); //此方法必须重写
        init();
    }

    private void init() {
        if (aMap == null) {
            aMap = mapView.getMap();
        }
        aMap.showIndoorMap(true);
    }

    public void drawMultiPoints(View view) {
        LoadTask loadTask = new LoadTask(this);
        loadTask.execute();
    }

    private void show(List<MultiPointItem> list) {
        MultiPointOverlayOptions overlayOptions = new MultiPointOverlayOptions();
        overlayOptions.icon(BitmapDescriptorFactory.fromResource(R.drawable.marker_blue));
        overlayOptions.anchor(0.5f,0.5f);

        MultiPointOverlay multiPointOverlay = aMap.addMultiPointOverlay(overlayOptions);
        multiPointOverlay.setItems(list);

        AMap.OnMultiPointClickListener multiPointClickListener = new AMap.OnMultiPointClickListener() {
            @Override
            public boolean onPointClick(MultiPointItem pointItem) {
                return false;
            }
        };
        aMap.setOnMultiPointClickListener(multiPointClickListener);
    }

    private class LoadTask extends AsyncTask<Void, Void, List<MultiPointItem>> {

        private MultiPointsActivity activity;

        public LoadTask(MultiPointsActivity activity) {
            this.activity = activity;
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            Toast.makeText(activity, "读取数据中...", Toast.LENGTH_LONG).show();
        }

        @Override
        protected List<MultiPointItem> doInBackground(Void... params) {
            return loadPoints10W();
        }

        @Override
        protected void onPostExecute(List<MultiPointItem> multiPointItems) {
            super.onPostExecute(multiPointItems);
            activity.show(multiPointItems);
        }

        private List<MultiPointItem> loadPoints10W() {
            List<MultiPointItem> list = new ArrayList<>();
            InputStream inputStream = null;
            try {
                inputStream = getResources().openRawResource(R.raw.point10w);
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
                String line = "";
                while((line = bufferedReader.readLine()) != null) {
                    if(isDestroyed()) {
                        return list;
                    }
                    String[] str = line.split(",");
                    double lat = Double.parseDouble(str[1].trim());
                    double lon = Double.parseDouble(str[0].trim());

                    LatLng latLng = new LatLng(lat, lon, false);//保证经纬度没有问题的时候可以填false

                    MultiPointItem multiPointItem = new MultiPointItem(latLng);
                    list.add(multiPointItem);
                }
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    if (inputStream != null) {
                        inputStream.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return list;
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        mapView.onResume();
    }

    /**
     * 方法必须重写
     */
    @Override
    protected void onPause() {
        super.onPause();
        mapView.onPause();
    }

    /**
     * 方法必须重写
     */
    @Override
    protected void onDestroy() {
        super.onDestroy();
        mapView.onDestroy();
    }

    /**
     * 方法必须重写
     */
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        mapView.onSaveInstanceState(outState);
    }
}
